package main;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String un = request.getParameter("uname");
		String pw = request.getParameter("passwd");
		String msg = "";
		Connection conn = null;
		String sql = "Select * from Customers where CustEmail=mike@medialounge.ca and CustPasswd=pass";
		try {
			
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/travelexperts","root","root");
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			if (rs.next())
			{
				response.sendRedirect("index.jsp");
			}
			else 
			{
				msg = "hello" + un + "it doesn't work.";
			}
			rs.close();
			stmt.close();
			}catch(Exception e) {
				
			}
	
//		System.out.println("Service Method");
//		String username="mike@medialounge.ca";
//		String password="pass";
//		
//		String un = request.getParameter("uname");
//		String pw = request.getParameter("passwd");
//		String msg = "";
//		
//		if(un.equals(username) && pw.equals(password))
//				{
//			msg="hello" + un + "Login Sucessful";
//				}
//		else {
//			msg="hello" + un + "Login Failed";
//		}
//		
//		response.setContentType("text/html");
//		PrintWriter out = response.getWriter();
//		out.println("<h2 style ='color: red;'>" + msg + "<h2>");
//		
//		
	}

}
